<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\activity\ActivityModel;
use think\Request;

/**
 * Activity
 * Class Activity
 * @package app\controller
 */
class Activity extends BaseEncryptController
{

    protected function initialize()
    {
        parent::initialize();
        $this->model = new ActivityModel();

        
    }

    /**
     * 分页列表
     * @param string $status
     * @param string $keyword
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     */
    public function getPage($status = '', $keyword = '', $page = 1, $limit = 10)
    {
        $map = [];
        if (!empty($status)) {
            $map['status'] = $status;
        }
        if (!empty($keyword)) {
            $map['like_field'] = [
                'name' => "%" . $keyword . "%"
            ];
        }
        $data = $this->model->getPageList($map, 'id DESC', $page, $limit);
        $dataList = $data['dataList'];
        foreach ($dataList as &$value) {
            $value['start_time_str'] = date("Y-m-d", $value['start_time']);
            $value['end_time_str'] = date("Y-m-d", $value['end_time']);
        }
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $dataList
        ]);
    }

    /**
     * 单条信息
     * @param $id
     * @return \think\response\Json
     */
    public function getInfo($id)
    {
        $item = $this->model->getInfo($id);
        $item['start_time'] = $item['start_time'] * 1000;
        $item['end_time'] = $item['end_time'] * 1000;
        if (!empty($item)) {
            return self::returnMsg(1, $item);
        } else {
            return self::returnMsg(1001);
        }
    }

    /**
     * 保存
     * @param Request $request
     * @return \think\response\Json
     */
    public function onSave(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'name' => $request->param('name'),
            'intro' => $request->param('intro'),
            'start_time' => $request->param('start_time') / 1000,
            'end_time' => $request->param('end_time') / 1000,
            'condition_time' => $request->param('condition_time'),
            'status' => $request->param('status')
        ];
        if (empty($formData['id'])) {
            $validate = 'app\validate\ActivityValidate.add';
        } else {
            $validate = 'app\validate\ActivityValidate';
        }
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, true, true);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function onDelete($id)
    {
        $flag = $this->model::destroy(['id' => $id]);
        if (false !== $flag) {
            $this->model->delRedis($id);
            $this->model->redis->zRem($this->model->redis_key . ':rank', $id);
            $this->model->addToRecycle($id, 'ActivityModel', '活动管理', $this->currentUser['user_id'], $this->currentUser['user_type'], 'activity');
            return self::returnMsg(1);
        } else {
            return self::returnMsg(1002);
        }
    }

    /**
     * 保存状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function onStatus(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'status' => $request->param('status'),
        ];
        $validate = 'app\validate\ActivityValidate.upstatus';
        $result = $this->validate($formData, $validate);

        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, false, false);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }
}
